package site.shihuan.helpdesk.im.manager;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import site.shihuan.helpdesk.common.util.IdWorker;
import site.shihuan.helpdesk.im.SessionContextHolder;
import site.shihuan.helpdesk.im.dao.SvcHiContactDao;
import site.shihuan.helpdesk.im.dao.SvcRuContactDao;
import site.shihuan.helpdesk.im.dao.SvcVisitorDao;
import site.shihuan.helpdesk.im.message.impl.ContactMessage;
import site.shihuan.helpdesk.im.model.Constants;
import site.shihuan.helpdesk.im.model.po.SvcHiContactPO;
import site.shihuan.helpdesk.im.model.po.SvcRuContactPO;
import site.shihuan.helpdesk.im.model.po.SvcVisitorPO;
import site.shihuan.helpdesk.im.model.vo.ClerkVO;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 访客管理
 *
 * @author 周世焕
 * @Date 2021/2/20 11:52
 */
@Component
@Slf4j
public class VisitorManager {

    @Autowired
    private SvcVisitorDao svcVisitorDao;

    @Autowired
    private SvcRuContactDao svcRuContactDao;

    /**
     * 查询该客户端是否已有记录
     *
     * @param tenantId
     * @param visitorId
     * @return
     */
    public SvcVisitorPO findVisitor(String tenantId,String visitorId) {
        return svcVisitorDao.selectByPrimaryKey(visitorId, tenantId);
    }

    /**
     * 创建新的Visitor
     * @param clientId
     */
    public SvcVisitorPO createVisitor(String clientId,String tenantId) {
        SvcVisitorPO visitorPO = new SvcVisitorPO();
        visitorPO.setTenantId(tenantId);
        visitorPO.setVisitorId(clientId);
        visitorPO.setOnline(Constants.STATUS_ONLINE);
        svcVisitorDao.insertSelective(visitorPO);
        return visitorPO;
    }

    /**
     * 改变在线状态
     * @param visitorId
     * @param tenantId
     * @param status
     */
    public void changeOnlineStatus(String visitorId, String tenantId,String status) {
        SvcVisitorPO visitorPO = new SvcVisitorPO();
        visitorPO.setVisitorId(visitorId);
        visitorPO.setTenantId(tenantId);
        visitorPO.setOnline(status);
        svcVisitorDao.updateByPrimaryKeySelective(visitorPO);
    }

    /**
     * 批量更改在线状态
     * @param collection
     * @param status
     */
    public void changeOnlineStatusBatch(Collection collection, String status) {
        svcVisitorDao.updateOnlineStatusBatch(collection, status);
    }

    /**
     * 查询当前该访客一对一联系的客服，没有则返回Null
     * @param visitorId
     */
    public ClerkVO findMyDeskClerk(String visitorId,String tenantId) {
        return svcRuContactDao.selectMyClerk(visitorId,tenantId);
    }
}
